import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;

import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;


public class Utils{

	/**
	 * @param args
	 */
	
	private static JFileChooser choose = null;
	private static Integer xis,yis;	
	private static String arquivoWave;
public Utils() {
}



public static String inverterBarra(String url){
	String teste = url.replaceAll("\\\\", "/");
	return teste;
}
public static BufferedImage readImage(String filename) throws IOException {
    return ImageIO.read(new File(filename));
}

public static void writeImage(BufferedImage image, String filetype, String filename) throws IOException {
    ImageIO.write(image, filetype, new File(filename));
}

public static BufferedImage[][] splitImage(BufferedImage image, int rows, int cols) {
    BufferedImage[][] tmp = new BufferedImage[rows][cols];
    int newWidth = image.getWidth() / cols;
    int newHeight = image.getHeight() / rows;
    //System.out.println("X:" + newWidth + " Y: " + newHeight );
    int m, n, x, y;
    for (m = 0; m < rows; m++) {
        y = newHeight * m;
        for (n = 0; n < cols; n++) {
            x = newWidth * n;
            tmp[m][n] = image.getSubimage(x, y, newWidth, newHeight);
        }
    }
    return tmp;
}








public static BufferedImage resizeImage(BufferedImage image, float escale) {
    int newWitdh = (int) (image.getWidth() * escale);
    int newHeight = (int) (image.getHeight() * escale);
    return resizeImage(image, newWitdh, newHeight);
}

public static BufferedImage resizeImage(BufferedImage image, float escaleWitdh, float escaleHeight) {
    int newWitdh = (int) (image.getWidth() * escaleWitdh);
    int newHeight = (int) (image.getHeight() * escaleHeight);
    return resizeImage(image, newWitdh, newHeight);
}

public static BufferedImage resizeImage(BufferedImage image, int newWitdh, int newHeight) {
    BufferedImage tmp = new BufferedImage(newWitdh, newHeight, image.getType());
    Graphics2D g2D = tmp.createGraphics();
    g2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2D.drawImage(image, 0, 0, newWitdh, newHeight, 0, 0, image.getWidth(), image.getHeight(), null);
    g2D.dispose();
    return tmp;
}



public static BufferedImage rotateImage(BufferedImage image, int angle) {
    return rotateImage(image, angle, image.getWidth() / 2, image.getHeight() / 2 );
}

public static BufferedImage rotateImage(BufferedImage image, int angle, int x, int y) {
    BufferedImage tmp = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
    Graphics2D g2D = tmp.createGraphics();
    g2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2D.rotate(Math.toRadians(angle), x, y);
    g2D.drawImage(image, null, 0, 0);
    return tmp;
}

	
	
	public static String getMappath(){
		arquivoWave = null;
        FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG,GIF, PNG Images", "jpg", "gif", "png");
        choose = new JFileChooser();
        choose.setFileFilter(filter);
        int returnVal = 0;
        returnVal = choose.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            arquivoWave = choose.getSelectedFile().getPath();
        }
        	if(arquivoWave != null){
        		return arquivoWave;
        	}
        	
        	return null;
//            imagem = ImageUtil.readImage(arquivoWave);
//            AreaGrafica.img = new BufferedImage[imagem.getHeight()/16][imagem.getWidth()/16]; //LINHAS E COLUNAS
//            imgrec = new Imagem[imagem.getHeight()/16][imagem.getWidth()/16];
//            imagem = ImageUtil.resizeImage(imagem, 240, 144);
		
	}



	public static String getCoordenada(int parseInt, int l, int c) {
		int resto = parseInt%c;
		if(resto>=1){
			xis = (parseInt/c);
			yis = resto-1;
		}else{
			xis = (parseInt/c)-1;
			yis = c - 1;
		}
		String coordenada = xis.toString() + "x" + yis.toString();
		//System.out.println(coordenada);
		return coordenada;
	}
	
	public static JFileChooser getChoose() {
		return choose;
	}
	
	public static void removeDuplicates(ArrayList<String> list) {
        HashSet<String> set = new HashSet<String>(list);
        list.clear();
        list.addAll(set);
}

	
	
}


